package com.sinoglobal.dumping.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sinoglobal.dumping.R;
import com.sinoglobal.dumping.base.Dumpling_DateUtils;
import com.sinoglobal.dumping.bean.myDumpling.Dumpling_MyCashDumplingBean.MyCashDumpling;

public class Dumpling_MakeCardAdapter extends BaseAdapter {

	private List<MyCashDumpling> datas;
	private Context context;
	
	public Dumpling_MakeCardAdapter(Context context) {
		this.context = context ;
	}

	@Override
	public int getCount() {
		
		return datas == null ? 0  : datas.size() + 1;
	}

	@Override
	public Object getItem(int position) {

		if (datas != null && position > 0) {
			return datas.get(position - 1);
		}
		
		return null ;
	}

	@Override
	public long getItemId(int position) {
		
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null ;
		if (position == 0) {
			convertView = View.inflate(context, R.layout.dumpling_item_my_cash_dumpling_header, null) ;
			TextView tv_make_cash_dumpling_totolprice = (TextView) convertView.findViewById(R.id.tv_make_cash_dumpling_totolprice);
			TextView tv_make_cash_dumpling_totolnumber = (TextView) convertView.findViewById(R.id.tv_make_cash_dumpling_totolnumber);
			tv_make_cash_dumpling_totolprice.setText("贺卡饺子100个");
			tv_make_cash_dumpling_totolnumber.setText("共包100张美图");
		} else {
		
			if (convertView == null || convertView instanceof RelativeLayout ) {
				viewHolder = new ViewHolder() ;
				convertView = View.inflate(context, R.layout.dumpling_item_my_cash_dumpling, null);
				viewHolder.tv_make_cash_dumpling_content = (TextView) convertView.findViewById(R.id.tv_make_cash_dumpling_content);
				viewHolder.tv_make_cash_dumpling_price = (TextView) convertView.findViewById(R.id.tv_make_cash_dumpling_price);
				viewHolder.tv_make_cash_dumpling_date = (TextView) convertView.findViewById(R.id.tv_make_cash_dumpling_date);
				viewHolder.tv_make_cash_dumpling_status = (TextView) convertView.findViewById(R.id.tv_make_cash_dumpling_status);
				viewHolder.tv_make_cash_dumpling_number = (TextView) convertView.findViewById(R.id.tv_make_cash_dumpling_number);
				viewHolder.ll_make_cash_dumpling_rootview = (LinearLayout) convertView.findViewById(R.id.ll_make_cash_dumpling_rootview);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			
			MyCashDumpling myCashDumpling = datas.get(position - 1);
			
			if (myCashDumpling.status == 3) {
				viewHolder.ll_make_cash_dumpling_rootview.setBackgroundColor(context.getResources().getColor(R.color.dumpling_white));
			} else {
				viewHolder.ll_make_cash_dumpling_rootview.setBackgroundColor(context.getResources().getColor(R.color.dumpling_gray6_e3e3e3));
			}
			
			String statusText = "" ;
			if (myCashDumpling.status == 3) {
				statusText = "未领完" ;
			} else if (myCashDumpling.status == 4) {
				statusText = "已领完" ;
			} else if (myCashDumpling.status == 5) {
				statusText = "已过期" ;
			}
			
			viewHolder.tv_make_cash_dumpling_content.setText(myCashDumpling.blessing);
			viewHolder.tv_make_cash_dumpling_price.setText(myCashDumpling.moneyNum +"");
			viewHolder.tv_make_cash_dumpling_date.setText(Dumpling_DateUtils.format(myCashDumpling.createDate, "MM-dd"));
			viewHolder.tv_make_cash_dumpling_status.setText(statusText);
			viewHolder.tv_make_cash_dumpling_number.setText(myCashDumpling.receiveNum+ "/" + myCashDumpling.dumplinglNum + "个");
		}
		return convertView;
	}

	public void setData(List<MyCashDumpling> datas) {
		this.datas = datas ;
		notifyDataSetChanged();
	}

	public static class ViewHolder {
		private TextView  tv_make_cash_dumpling_content ;
		private TextView  tv_make_cash_dumpling_price ;
		private TextView  tv_make_cash_dumpling_date ;
		private TextView  tv_make_cash_dumpling_status ;
		private TextView  tv_make_cash_dumpling_number ;
		private LinearLayout ll_make_cash_dumpling_rootview ;
	}

	public void setDataList(List<MyCashDumpling> resultList) {
		this.datas.addAll(resultList);
		notifyDataSetChanged();
		
	} 

}
